<?php
// +----------------------------------------------------------------------
// | HisiPHP框架[基于ThinkPHP5.1开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2021 http://www.hisiphp.com
// +----------------------------------------------------------------------
// | HisiPHP承诺基础框架永久免费开源，您可用于学习和商用，但必须保留软件版权信息。
// +----------------------------------------------------------------------
// | Author: 橘子俊 <364666827@qq.com>，开发者QQ群：50304283
// +----------------------------------------------------------------------
namespace app\system\model;

use think\Db;
use think\Model; 

/**
 * 后台代理模型
 * @package app\system\model
 */
class SystemDaili extends Model
{ 
    /**
     * 获取等级列表
     */
    public function getLevelList(){
       return Db::name('dldj')->select();
    }
    
    /**
     * 获取代理等级详情
     */
    public function getLevelInfo($id){
        return Db::name('dldj')->where("id = '{$id}'")->find();
    }
    
    /**
     * 更新代理等级
     */
    public function levelUpdate($data){
        return Db::name('dldj')->update($data);
    }
    
    /**
     * 获取代理详情
     */
    public function getDailiInfo($id){
        $daili_info = Db::name('daili')->where("id = '{$id}'")->find();
        $province = Db::name('city')->where("code = '{$daili_info['province']}' and p_code = 0")->value('name');
        $city = Db::name('city')->where("code = '{$daili_info['city']}' and p_code != 0")->value('name');
        $area = Db::name('city')->where("code = '{$daili_info['area']}' and p_code != 0")->value('name');
        $daili_info['diqu'] = $province." ".$city." ".$area;
        $daili_info['fj_daili'] = Db::name('daili')->where("id = '{$daili_info['pid']}'")->find();
        $daili_info['dldj'] = Db::name('dldj')->where("id = '{$daili_info['lddj_id']}'")->value('levelname');
        $daili_info['fj_dldj'] = Db::name('dldj')->where("id = '{$daili_info['fj_daili']['lddj_id']}'")->value('levelname');
        return $daili_info;
    }
    
    /**
     * 更新代理信息
     */
    public function updateDaili($data){
        return Db::name('daili')->update($data);
    }
    
    /**
     * 代理审核
     */
    public function shenheDaili($data){
        $daili_info = Db::name('daili')->where("id = '{$data['id']}'")->find();
        //父级分佣，平级或下级（ztbl），越级（ytbl）
        $fj_daili_info = Db::name('daili')->where("id = '{$daili_info['pid']}'")->find();
        $fj_dldj_info = Db::name('dldj')->where("id = '{$fj_daili_info['lddj_id']}'")->find();
        if($daili_info['lddj_id'] <= $fj_daili_info['lddj_id']){
            $fj_fy_data['cz_flje'] = $fj_dldj_info['ztbl']*$daili_info['scje']/100;
            $fj_fy_data['fl_type'] = 1;//父级直推
            $fj_fy_data['fxbl'] = $fj_dldj_info['ztbl'];
        }else if($daili_info['lddj_id'] == $fj_daili_info['lddj_id']+1){
            //越级分佣（只能越一级）
            $fj_fy_data['cz_flje'] = $fj_dldj_info['ytbl']*$daili_info['scje']/100;
            $fj_fy_data['fl_type'] = 2;//父级越级
            $fj_fy_data['fxbl'] = $fj_dldj_info['ytbl'];
        }
        $fj_fy_data['czjl_id'] = $daili_info['id'];
        $fj_fy_data['dl_id'] = $fj_daili_info['id'];
        $fj_fy_data['cz_flsj'] = time(); 
        Db::name("czfljl")->insert($fj_fy_data);
        Db::name('daili')->where("id = '{$daili_info['pid']}'")->setInc('yongjin', $fj_fy_data['cz_flje']);
        //上上级分佣（jtbl），同级和下级
        if($daili_info['ppid']){
            $ssj_daili_info = Db::name('daili')->where("id = '{$daili_info['ppid']}'")->find();
            $ssj_dldj_info = Db::name('dldj')->where("id = '{$ssj_daili_info['lddj_id']}'")->find();
            if($daili_info['lddj_id'] <= $ssj_daili_info['lddj_id']){
                $ssj_fy_data['cz_flje'] = $ssj_dldj_info['jtbl']*$daili_info['scje']/100;
                $ssj_fy_data['fxbl'] = $ssj_dldj_info['jtbl'];
                $ssj_fy_data['fl_type'] = 3;//上上级分佣
                $ssj_fy_data['czjl_id'] = $daili_info['id'];
                $ssj_fy_data['dl_id'] = $ssj_daili_info['id'];
                $ssj_fy_data['cz_flsj'] = time();
                Db::name("czfljl")->insert($ssj_fy_data);
                Db::name('daili')->where("id = '{$ssj_daili_info['id']}'")->setInc('yongjin', $ssj_fy_data['cz_flje']);
            }
        }
        $sh_data['shy_id'] = ADMIN_ID;
        $sh_data['sh_status'] = 1;
        $sh_data['sh_time'] = time();
        $djdm = array('','D','C','B','A');
        $sh_data['sqh'] = $djdm[$daili_info['lddj_id']].sprintf("%06d", $daili_info['id']);
        $sh_data['sqs_path'] = "";
        Db::name('daili')->where("id = '{$data['id']}'")->update($sh_data);
        return TRUE;
    }
    
    /**
     * 获取代理列表
     */
    public function getDailiList($where){
        $list = Db::name('daili')->where($where)->select();
        foreach($list as $key => $val){
            $list[$key]['levelname'] = Db::name('dldj')->where("id = '{$val['lddj_id']}'")->value('levelname');
            $list[$key]['province_text'] = Db::name('city')->where("code = '{$val['province']}' and p_code = 0")->value('name');
            $list[$key]['city_text'] = Db::name('city')->where("code = '{$val['city']}'  and p_code != 0")->value('name');
            $list[$key]['area_text'] = Db::name('city')->where("code = '{$val['area']}'")->value('name');
        } 
        return $list;
    }
    
    /**
     * 采购审核
     */
    public function shenheCgjl($data){
        $cgjl_info = Db::name("cgjl")->where("id = '{$data['id']}'")->find();
        //采购者信息
        $dqdl_info = Db::name("daili")->where("id = '{$cgjl_info['dl_id']}'")->find();
        //上级代理信息(被返现用户)
        $sjdl_info = Db::name("daili")->where("id = '{$dqdl_info['pid']}'")->find();
        $sjdl_dj_info = Db::name("dldj")->where("id = '{$sjdl_info['lddj_id']}'")->find();
        
        if($sjdl_info['lddj_id'] == $dqdl_info['lddj_id']){
            //平级
            $fl_data['fldj'] = $sjdl_dj_info['cgfl_pj'];
            $fl_data['fl_type'] = 1;
        }else if($sjdl_info['lddj_id'] == $dqdl_info['lddj_id']+1){
            //下级（1）
            $fl_data['fldj'] = $sjdl_dj_info['cgfl_xj'];
            $fl_data['fl_type'] = 3;
        }else if($sjdl_info['lddj_id'] == $dqdl_info['lddj_id']+2){
            //下级（2）
            $fl_data['fldj'] = $sjdl_dj_info['cgfl_xj1'];
            $fl_data['fl_type'] = 3;
        }else if($sjdl_info['lddj_id'] == $dqdl_info['lddj_id']+3){
            //下级（3）
            $fl_data['fldj'] = $sjdl_dj_info['cgfl_xj2'];
            $fl_data['fl_type'] = 3;
        }else if($sjdl_info['lddj_id']+1 == $dqdl_info['lddj_id']){
            //越级
            $fl_data['fldj'] = $sjdl_dj_info['cgfl_yj'];
            $fl_data['fl_type'] = 2;
        }else{
            $sh_data['cw_id'] = ADMIN_ID;
            $sh_data['pay_status'] = 1;
            $sh_data['shsj'] = time();
            Db::name('cgjl')->where("id = '{$data['id']}'")->update($sh_data);
            return TRUE;
            exit();
        } 
        $fl_data['cggs'] = $cgjl_info['cggs'];
        $fl_data['cg_flje'] = $fl_data['cggs']*$fl_data['fldj'];
        $fl_data['cg_flsj'] = time();
        $fl_data['cgjl_id'] = $data['id'];
        $fl_data['dl_id'] = $sjdl_info['id'];
        
        Db::name("cgfl")->insert($fl_data);
        
        $sh_data['cw_id'] = ADMIN_ID;
        $sh_data['pay_status'] = 1;
        $sh_data['shsj'] = time(); 
        Db::name('cgjl')->where("id = '{$data['id']}'")->update($sh_data);
        Db::name('daili')->where("id = '{$sjdl_info['id']}'")->setInc('yongjin', $fl_data['cg_flje']);
        Db::name('daili')->where("id = '{$cgjl_info['dl_id']}'")->setInc('xtgs', $cgjl_info['cggs']);
        return TRUE;
    }
}
